<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" >
      <el-form-item label="班级名称" prop="name">
        <el-input v-model="queryParams.name" class="input-w-180" placeholder="请输入班级名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="级数" prop="grade">
        <el-input v-model="queryParams.grade" class="input-w-100" placeholder="请输入级数" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="带教老师" prop="teacher">
        <el-input v-model="queryParams.teacher" class="input-w-180" placeholder="请输入带教老师" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="开始时间">
        <el-date-picker v-model="daterangeStartTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间">
        <el-date-picker v-model="daterangeEndTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="人数限制" prop="quantity">
        <el-input v-model="queryParams.quantity" class="input-w-180" placeholder="请输入人数限制" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="班级状态" prop="status">
        <el-select v-model="queryParams.status"  class="input-w-180" placeholder="请选择班级状态" clearable>
          <el-option v-for="dict in dict.type.pv_training_class_status" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="daterangeCreateTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-info" size="mini" @click="invitation.open = true">邀请注册</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['pv:training_class:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['pv:training_class:remove']">删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['pv:training_class:export']">导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="training_classList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="班级名称" align="center" prop="name">
        <template slot-scope="scope">
          <router-link :to="'/pv/training_class_student/index/' + scope.row.id" class="link-type">
            <span>{{ scope.row.name }}</span>
          </router-link>
        </template>
      </el-table-column>
<!--      <el-table-column label="级数" align="center" prop="grade"/>-->
      <el-table-column label="带教老师" align="center" prop="teacher"/>
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="人数限制" align="center" prop="quantity"/>
      <el-table-column label="班级状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pv_training_class_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="info"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['pv:training_class:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['pv:training_class:remove']">删除</el-button>
<!--          <el-button size="mini" type="text" icon="el-icon-message" @click="handleSms(scope.row)" v-hasPermi="['pv:training_class:edit']">短信通知</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改培训班管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="班级名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入班级名称" style="width: 250px"/>
        </el-form-item>
<!--        <el-form-item label="级数" prop="grade">-->
<!--          <el-input v-model="form.grade" placeholder="请输入级数" style="width: 250px"/>-->
<!--        </el-form-item>-->
        <el-form-item label="带教老师" prop="teacher">
          <el-input v-model="form.teacher" placeholder="请输入带教老师" style="width: 250px"/>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable v-model="form.startTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择开始时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable v-model="form.endTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择结束时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="人数限制" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入人数限制" style="width: 250px"/>
        </el-form-item>
        <el-form-item label="班级状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择班级状态">
            <el-option v-for="dict in dict.type.pv_training_class_status" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="info">
          <el-input v-model="form.info" type="textarea" placeholder="请输入内容" style="width: 250px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-popover style="margin-right: 10px" v-if="form.status == '2'" v-model="visible" placement="bottom" width="320">
          <p>毕业将注销未报名学生的证件有效期以及上岗证，请确认？</p>
          <div style="text-align: right; margin: 0">
            <el-button size="mini" type="text" @click="visible = false">取 消</el-button>
            <el-button type="primary" size="mini" @click="submitForm">确 定</el-button>
          </div>
          <el-button type="primary" slot="reference">确 定</el-button>
        </el-popover>

        <el-button v-else type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 邀请注册弹窗 -->
    <el-dialog title="邀请学员下载" :visible.sync="invitation.open" width="400px" append-to-body>
      <div class="demonstration">      &nbsp;  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 打开浏览器通过扫描二维码的方式下载平台</div>
      <div style="margin-top: 30px">
        <div class="invitation-in-class" style="margin-left: 30px">
          <img width="300px" height="300px" src="http://obs.jy-medtechs.com/postpartum/xiazai.png" alt="下载链接">

        </div>
      </div>
    </el-dialog>

    <!-- 短信通知弹窗 -->
    <el-dialog title="发送短信通知" :visible.sync="sms.open" width="700px" append-to-body>
      <el-row v-loading="loading">
        <el-col :span="15">
          <div style="width: 50%;position: relative;float: left;">
            <img :src="phoneSms" style="width: 300px;height: 600px" alt="">
            <p style="width: 200px;position:absolute;top:34%;left:56%;transform: translate(-50%, -50%);font-weight: bold;font-size: 15px;">
              【浦东新区妇幼保健中心】{{ sms.content }}</p>
          </div>
        </el-col>
        <el-col :span="5" style="float: left;margin-top: 200px">
          <el-button :type="sms.sended ? 'danger': 'primary'" icon="el-icon-message" @click="sendSms" :disabled="sms.sended" style="margin-top: 30px;width: 200px;height: 100px">
            {{sms.sended ? '已发送' : '发送'}}
          </el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTraining_class,
  delTraining_class,
  addTraining_class,
  updateTraining_class, smsSend, smsSend_people_count
} from "@/api/pv/training_class";
import wechat_logo from '@/assets/logo/wechat_log.jpg'
import {copyObj, strFormat} from "@/utils/zzDouUtils/ObjUtil";
import phoneSms from "@/assets/images/phone_sms.png";
import {parseTime} from "@/utils/ruoyi";

export default {
  name: "Training_class",
  dicts: ['pv_training_class_status'],
  data() {
    return {
      visible:false,
      wechat_logo:wechat_logo,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 培训班管理表格数据
      training_classList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 描述时间范围
      daterangeStartTime: [],
      // 描述时间范围
      daterangeEndTime: [],
      // 描述时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        grade: null,
        teacher: null,
        startTime: null,
        endTime: null,
        quantity: null,
        status: null,
        info: null,
        createTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "班级名称不能为空", trigger: "blur"}
        ],
      },
      //邀请注册
      invitation: {
        open: false,
      },
      phoneSms: phoneSms,
      //短信通知
      sms:{
        open: false,
        clazz: null,
        sended: false,//是否已经发送
        content: '',
        peopleCount:0,
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    goto(url){
      window.open(url)
    },
    /** 查询培训班管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeStartTime && '' != this.daterangeStartTime) {
        this.queryParams.params["beginStartTime"] = this.daterangeStartTime[0];
        this.queryParams.params["endStartTime"] = this.daterangeStartTime[1];
      }
      if (null != this.daterangeEndTime && '' != this.daterangeEndTime) {
        this.queryParams.params["beginEndTime"] = this.daterangeEndTime[0];
        this.queryParams.params["endEndTime"] = this.daterangeEndTime[1];
      }
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listTraining_class(this.queryParams).then(response => {
        this.training_classList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        grade: null,
        teacher: null,
        startTime: null,
        endTime: null,
        quantity: null,
        status: null,
        info: null,
        reservedOne: null,
        reservedTwo: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeStartTime = [];
      this.daterangeEndTime = [];
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加培训班管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.form = copyObj(row);
      this.open = true;
      this.title = "修改培训班管理";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.visible = false;
          if (this.form.id != null) {
            updateTraining_class(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTraining_class(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除培训班管理编号为"' + ids + '"的数据项？').then(function () {
        return delTraining_class(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pv/training_class/export', {
        ...this.queryParams
      }, `training_class_${new Date().getTime()}.xlsx`)
    },
    /** 打开短信通知弹窗 */
    handleSms(row){
      this.sms.clazz = copyObj(row);
      this.sms.content = strFormat('张三您好，${year}产褥期保健复训班计划于${date},08:00:00开班，请在${date1}前,登录微信小程序“公卫处方权培训基地”，完善个人信息，进行线上报名，并按期完成培训和考试。小程序登录名为上岗证编号。',{
        year: parseTime(new Date(),'{y}'),
        date: parseTime(row.startTime,'{y}-{m}-{d}'),
        date1: parseTime(row.startTime,'{y}-{m}-{d}'),
      });
      //查询人数
      smsSend_people_count().then(response=>{
        this.sms.peopleCount = response.data;
      })
      this.sms.sended = row.reservedTwo == '1';
      this.sms.open = true;
    },
    /** 发送短信 */
    sendSms(){
        this.$confirm(`预计发送短信人数为 ${this.sms.peopleCount} 人，是否确认发送`, '提示', {
          confirmButtonText: '发送',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true;
          smsSend(this.sms.clazz.id).then(response=>{
            this.sms = {open: false, clazz: null, sended: false, content: '',peopleCount:0}
            this.$modal.msgSuccess("发送成功")
            this.loading = false;
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消成功'
          });
        });
    }
  }
};
</script>
